package com.ph.irp.sell.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ph.irp.base.currency.dao.PBaseCurrencyDAO;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCustomerDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectDepartmentDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectMaterialDao;
import com.ph.irp.dto.SellPriceDTO;
import com.ph.irp.dto.SellPriceDetailDTO;
import com.ph.irp.model.PBaseCurrency;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.irp.model.PBaseVerifyprojectCustomer;
import com.ph.irp.model.PBaseVerifyprojectDepartment;
import com.ph.irp.model.PBaseVerifyprojectMaterial;
import com.ph.irp.model.PSellSellprice;
import com.ph.irp.model.PSellSellpricedetail;
import com.ph.irp.sell.dao.SellPriceDao;
import com.ph.irp.sell.dao.SellPriceDetailDao;
import com.ph.irp.sell.service.SellPriceService;

public class SellPriceServiceImpl implements SellPriceService{

	private SellPriceDao sellPriceDaoProxy;
	private PBaseVerifyprojectCustomerDao customerDaoProxy;
	private PBaseVerifyprojectCandiateDao managerDaoProxy;
	private PBaseVerifyprojectCandiateDao businesserDaoProxy;
	private PBaseVerifyprojectDepartmentDao departmentDaoProxy;
	private PBaseCurrencyDAO currencyDaoProxy;
	private SellPriceDetailDao sellPriceDetailDaoProxy;
	private PBaseVerifyprojectMaterialDao materialDaoProxy;

	
	/**
	 * 
	 * 新增
	 */
	public void addSellPrice(PSellSellprice sellprice,List<PSellSellpricedetail> sellPriceDetailList)throws Throwable{
		

		//保存采购订单
		sellPriceDaoProxy.addSellPrice(sellprice);
		//保存过得采购订单id
		int sellPriceId = sellPriceDaoProxy.getSellPriceId(sellprice.getSellPriceCode());
		//保存采购订单明细

		for (int i=0;i<sellPriceDetailList.size();i++){
			sellPriceDetailList.get(i).setSellPriceId(sellPriceId) ;
			sellPriceDetailDaoProxy.addSellPriceDetail(sellPriceDetailList.get(i));
			
		}
		
	}
	/**
	 * 
	 * 查询sellPriceByID
	 */
	public PSellSellprice getSellPriceById(int sellPriceId) throws Throwable{
		
		return this.sellPriceDaoProxy.getSellPriceById(sellPriceId);
	}
	
	
	/**
	 * 
	 * 查询sellPriceDTOByID
	 */
	public List<SellPriceDTO> getSellPriceDTOById(int sellPriceId) throws Throwable{
		
		return this.convertList(sellPriceDaoProxy.getSellPriceListById(sellPriceId), new ArrayList<SellPriceDTO>());
	}
	
	/**
	 * 
	 * 查询所有
	 */
	public List<SellPriceDTO> getAllSellPrice()throws Throwable{
		
		return this.convertList(sellPriceDaoProxy.getAllSellPrice(), new ArrayList<SellPriceDTO>());
	}
	/**
	 * 
	 * 删除SellPrice
	 */
	public void deleteSellPrice(Object[] id) throws Throwable{
		for (int i = 0; i < id.length; i++) {
			
			this.sellPriceDetailDaoProxy.deleteSellPriceDetail(Integer.valueOf((String) id[i]));
			
			this.sellPriceDaoProxy.deleteSellPrice(Integer.valueOf((String) id[i]));
	}
	}
	
	/**
	 * 
	 * 查询SellPriceDetailBySellPriceId
	 */

	public List<SellPriceDetailDTO> getSellPriceDetailBySellPriceId(int sellPriceId)throws Throwable{
		
		return this.convertListForDetail(this.sellPriceDetailDaoProxy.getSellPriceDetailBySellPriceId(sellPriceId), new ArrayList<SellPriceDetailDTO>());
	}

	/**
	 * 
	 * 查询ByCondition
	 */	
	
	public List<SellPriceDTO> getSellPriceByCondition(PSellSellprice sellprice)throws Throwable{
		
		List<SellPriceDTO> sellPriceDTOList = new ArrayList<SellPriceDTO>();
		List<PSellSellprice> list = this.sellPriceDaoProxy.getSellPriceByCondition(sellprice);
		sellPriceDTOList = this.convertList(list, sellPriceDTOList);
		
		return sellPriceDTOList;
	}
	
	
	/**
	 * 
	 * 新增销售报价明细
	 */
	public void addSellPriceDetail(PSellSellpricedetail sellpricedetail)throws Throwable{
		
		this.sellPriceDetailDaoProxy.addSellPriceDetail(sellpricedetail);
	
	}
	
	/**
	 * 
	 * 查询最新的code
	 */
	public String getNewestSellPriceCode() throws Throwable{
		return this.sellPriceDaoProxy.getNewestSellPriceCode();
	}
	
	
	
	
	
	public SellPriceDetailDao getSellPriceDetailDaoProxy() {
		return sellPriceDetailDaoProxy;
	}

	public void setSellPriceDetailDaoProxy(
			SellPriceDetailDao sellPriceDetailDaoProxy) {
		this.sellPriceDetailDaoProxy = sellPriceDetailDaoProxy;
	}


	public PBaseVerifyprojectMaterialDao getMaterialDaoProxy() {
		return materialDaoProxy;
	}

	public void setMaterialDaoProxy(PBaseVerifyprojectMaterialDao materialDaoProxy) {
		this.materialDaoProxy = materialDaoProxy;
	}
	
	
	public SellPriceDao getSellPriceDaoProxy() {
		return sellPriceDaoProxy;
	}


	public void setSellPriceDaoProxy(SellPriceDao sellPriceDaoProxy) {
		this.sellPriceDaoProxy = sellPriceDaoProxy;
	}


	public PBaseVerifyprojectCustomerDao getCustomerDaoProxy() {
		return customerDaoProxy;
	}


	public void setCustomerDaoProxy(PBaseVerifyprojectCustomerDao customerDaoProxy) {
		this.customerDaoProxy = customerDaoProxy;
	}

	public PBaseVerifyprojectCandiateDao getManagerDaoProxy() {
		return managerDaoProxy;
	}
	public void setManagerDaoProxy(PBaseVerifyprojectCandiateDao managerDaoProxy) {
		this.managerDaoProxy = managerDaoProxy;
	}
	public PBaseVerifyprojectCandiateDao getBusinesserDaoProxy() {
		return businesserDaoProxy;
	}
	public void setBusinesserDaoProxy(
			PBaseVerifyprojectCandiateDao businesserDaoProxy) {
		this.businesserDaoProxy = businesserDaoProxy;
	}
	public PBaseCurrencyDAO getCurrencyDaoProxy() {
		return currencyDaoProxy;
	}


	public void setCurrencyDaoProxy(PBaseCurrencyDAO currencyDaoProxy) {
		this.currencyDaoProxy = currencyDaoProxy;
	}

	public PBaseVerifyprojectDepartmentDao getDepartmentDaoProxy() {
		return departmentDaoProxy;
	}


	public void setDepartmentDaoProxy(
			PBaseVerifyprojectDepartmentDao departmentDaoProxy) {
		this.departmentDaoProxy = departmentDaoProxy;
	}

	
	
	/**
	 * 将model转化为dto
	 * sellPrice
	 * @param 
	 * @param 
	 * @return
	 * @throws Throwable 
	 */
	private List<SellPriceDTO> convertList(List<PSellSellprice> sellPriceList,List<SellPriceDTO> sellPriceDTOList) throws Throwable{	
		
		
		
		for( int i=0;i<sellPriceList.size();i++){			
			SellPriceDTO sellDto = new SellPriceDTO();	
			
			sellDto.setSellprice(sellPriceList.get(i));

			
			
			PBaseVerifyprojectCustomer customer = customerDaoProxy.getCustomerById(sellPriceList.get(i).getCustomerId());
			sellDto.setCustomer(customer);
			
			PBaseVerifyprojectCandiate manager = managerDaoProxy.getCandiateById(sellPriceList.get(i).getManagerId());
			sellDto.setManager(manager);	
			
			PBaseVerifyprojectDepartment department = departmentDaoProxy.getDepartmentById(sellPriceList.get(i).getDepartmentId());
			sellDto.setDepartment(department);
			
			PBaseVerifyprojectCandiate businesser = businesserDaoProxy.getCandiateById(sellPriceList.get(i).getBusinesserId());
			sellDto.setBusinesser(businesser);
			
			PBaseCurrency currency = currencyDaoProxy.getCurrencyByID(sellPriceList.get(i).getCurrencyId());
			sellDto.setCurrency(currency);
			
			sellPriceDTOList.add(sellDto) ;	
		}
		
		return sellPriceDTOList ;
	}
	/**
	 * 将model转化为dto
	 * sellPriceDetail
	 * @param 
	 * @param 
	 * @return
	 * @throws Throwable 
	 */
	private List<SellPriceDetailDTO> convertListForDetail(List<PSellSellpricedetail> sellPriceDetailList,List<SellPriceDetailDTO> sellPriceDetailDTOList) throws Throwable{	
		
		
		
		for( int i=0;i<sellPriceDetailList.size();i++){			
			SellPriceDetailDTO sellDetailDto = new SellPriceDetailDTO();	
			
			sellDetailDto.setSellpricedetail(sellPriceDetailList.get(i));

			
			
			PBaseVerifyprojectMaterial material = materialDaoProxy.getMaterialById(sellPriceDetailList.get(i).getMaterialId());
			sellDetailDto.setMaterial(material);
			
			PSellSellprice sellprice = sellPriceDaoProxy.getSellPriceById(sellPriceDetailList.get(i).getSellPriceId());
			sellDetailDto.setSellprice(sellprice);	
			

			
			sellPriceDetailDTOList.add(sellDetailDto) ;	
		}
		
		return sellPriceDetailDTOList ;
	}
	
}
